import 'dart:async';

import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';

import 'gaps.dart';

class Button extends StatelessWidget {
  final GestureTapCallback? onTap;
  final Color background;
  final BorderRadius borderRadius;
  final Widget child;
  final EdgeInsetsGeometry? padding;
  final EdgeInsetsGeometry? margin;
  final AlignmentGeometry? alignment;
  final Gradient? gradient;
  final Border? border;
  final bool canRequestFocus;
  final String? text;
  final int? interval; //点击间隔

  Button({
    Key? key,
    required this.child,
    this.text,
    this.onTap,
    this.background = Colors.grey,
    this.borderRadius = BorderRadius.zero,
    this.padding,
    this.margin,
    this.alignment,
    this.gradient,
    this.border,
    this.interval = 1000,
    this.canRequestFocus = false,
  }) : super(key: key);

  Button.text(
      {super.key,
      required this.text,
      TextStyle? textStyle,
      int maxLine = 1,
      this.onTap,
      this.background = Colors.grey,
      this.borderRadius = BorderRadius.zero,
      this.padding,
      this.margin,
      this.alignment,
      this.gradient,
      this.border,
      this.interval = 1000,
      this.canRequestFocus = false})
      : this.child = Text(
          text ?? '',
          style: textStyle,
          overflow: TextOverflow.ellipsis,
          maxLines: maxLine,
        );

  Button.image(
      {super.key,
      Widget? prefix,
      Widget? suffix,
      this.text,
      TextStyle? textStyle,
      int maxLine = 1,
      this.onTap,
      this.background = Colors.grey,
      this.borderRadius = BorderRadius.zero,
      this.padding,
      this.margin,
      this.alignment,
      this.gradient,
      this.border,
      this.interval = 1000,
      this.canRequestFocus = false})
      : this.child = Row(mainAxisSize: MainAxisSize.min, children: [
          prefix ?? Gaps.empty,
          ObjectUtil.isEmpty(text)
              ? Gaps.empty
              : Flexible(
                  child: Text(
                  text!,
                  style: textStyle,
                  maxLines: maxLine,
                  overflow: TextOverflow.ellipsis,
                )),
          suffix ?? Gaps.empty,
        ]);

  Button.icon(
      {super.key,
      Widget? top,
      this.text,
      TextStyle? textStyle,
      int maxLine = 1,
      this.onTap,
      this.background = Colors.grey,
      this.borderRadius = BorderRadius.zero,
      this.padding,
      this.margin,
      this.alignment,
      this.gradient,
      this.border,
      this.interval = 1000,
      this.canRequestFocus = false})
      : this.child = Column(mainAxisSize: MainAxisSize.min, children: [
          top ?? Gaps.empty,
          ObjectUtil.isEmpty(text)
              ? Gaps.empty
              : Text(
                  text!,
                  style: textStyle,
                  overflow: TextOverflow.ellipsis,
                  maxLines: maxLine,
                  textAlign: TextAlign.center,
                ),
        ]);

  Timer? _timer;

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        if (_timer == null) {
          _timer = Timer.periodic(Duration(milliseconds: interval ?? 1000), (timer) {
            _timer?.cancel();
            _timer = null;
          });
          onTap?.call();
        }
      },
      canRequestFocus: canRequestFocus,
      child: Container(
        alignment: alignment,
        padding: padding,
        margin: margin,
        decoration: BoxDecoration(color: background, borderRadius: borderRadius, gradient: gradient, border: border),
        child: child,
      ),
    );
  }
}
